package workers

import (
	"black_mamba/global"
	"fmt"
	"sync"
)

type Worker struct {
	Wg          sync.WaitGroup
	logCallback interface{}
	Topic       string
}

type worker func(data map[string]interface{})
type callback func(data int) int

//func (b *Worker) Decorator(w interface{}, options interface{}) func() {
//func (b *Worker) Decorator(w interface{}, options map[string]interface{}) func() {
//	b.Wg.Add(1)
//	return func() {
//		w.(func(map[string]interface {}))(options)
//		//w.(func(interface{}))(options)
//		b.Wg.Done()
//
//	}
//}

//func (b *Worker) Decorator(w interface{}, options map[string]interface{}, Wg *sync.WaitGroup) func() {
//	Wg.Add(1)
//	return func() {
//		w.(func(map[string]interface{}))(options)
//		//w.(func(interface{}))(options)
//		Wg.Done()
//	}
//}

func (w *Worker) Decorator(worker interface{}, options map[string]interface{}, Wg *sync.WaitGroup, ch chan interface{}) func() {
	Wg.Add(1)
	return func() {
		result := worker.(func(map[string]interface{}) interface{})(options)
		Wg.Done()
		if ch != nil {
			ch <- result
		}
	}
}

//func (b *Worker) SetLogCallback(callback interface{}) {
//	b.logCallback = callback
//}

func (w *Worker) sendLog(logData map[string]string) {
	//global.BM_QUEUE.Enqueue(logData)
	//global.BM_EVENTBUS.Publish(b.Topic, int(uintptr(unsafe.Pointer(&logData))))
	global.BM_EVENTBUS.Publish(fmt.Sprintf("%s:log", w.Topic), logData)
	if w.Topic == "deals" {
		//go func() {
		//	global.BM_API.SendLogToServer(global.BM_CONFIG.Section("basic").Key("serialNumberCiphertext").Value(), logData["content"], logData["email"], logData["status"])
		//}()
		global.BM_EVENTBUS.Publish("deals:updateData", map[string]interface{}{
			"group": logData["group"],
			"info":  logData["content"],
		})

	} else if w.Topic == "register" {
		global.BM_EVENTBUS.Publish("register:updateData", map[string]interface{}{
			"email": logData["email"],
			"info":  logData["content"],
		})
	}

	//xc.XC_CallUiThread(b.logCallback.(func(int) int), int(uintptr(unsafe.Pointer(&logData))))
}
